package com.algorithm.sort;

/**
 * @author msi
 * 冒泡排序
 */
public class BubbleSort {

    public static void main(String[] args) {
        int [] arr = {1,4,2,9,80,6,7,5,-1,56,-20};
        bubbleSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+",");
        }
    }
    public static void bubbleSort(int [] arr){
        if(arr == null || arr.length < 2){
            return;
        }
        for (int i = arr.length-1 ; i >0 ; i--){
            for (int j = 0; j<i; j++){
                if(arr[j]>arr[j+1]){
                    swap(arr,j,j+1);
                }
            }
        }
    }

    // ^ 异或
    // int a = a ^ b ^ b; b^b=0; a^0=a;
    private static void swap(int[] arr, int j, int i) {
        arr[i] = arr[i] ^ arr[j];
        arr[j] = arr[i] ^ arr[j];
        arr[i] = arr[i] ^ arr[j];
    }
}
